//
//  UIImageView+YTAdd.m
//  tesDemo02
//
//  Created by 拓客营 on 2022/3/21.
//

#import "UIImageView+YTAdd.h"

@implementation UIImageView (YTAdd)

- (void)yt_setIconImageURL:(NSString *)url {
   
    
//    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[TUIKitConfig.defaultConfig defaultAvatarImage] options:SDWebImageHandleCookies];
    //TODO: 用户头像
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage new] options:SDWebImageHandleCookies];
}

-(void)yt_setDefaultPlaceHolderWithImageURL:(NSString *)url {
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"icon_placeHolder"] options:SDWebImageHandleCookies];
}

- (void)yt_setImageURL:(NSString *)url {
    
    [self sd_setImageWithURL:[self handleImageURL:url] placeholderImage:nil options:SDWebImageHandleCookies];
}

- (void)yt_setImageURL:(NSString *)url placeHolder:(NSString *)placeHolder {
    
    [self sd_setImageWithURL:[self handleImageURL:url] placeholderImage:[UIImage imageNamed:placeHolder] options:SDWebImageHandleCookies];
}


- (void)yt_setBlurImageWithURLString:(NSString *)url radius:(CGFloat)radius {
    
    NSString *path = [[UIApplication sharedApplication].cachesPath stringByAppendingPathComponent:@"BK.Blur"];
    YYImageCache *cache = [[YYImageCache alloc] initWithPath:path];
    YYWebImageManager *manager = [[YYWebImageManager alloc] initWithCache:cache queue:[YYWebImageManager sharedManager].queue];
    manager.sharedTransformBlock = ^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
        
        if (image == nil) {
            return nil;
        }
        
        CIContext *context = [CIContext contextWithOptions:nil];
        CIImage *ciImage = [CIImage imageWithCGImage:[image resizeByDefaultInset].CGImage];
        CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [filter setValue:ciImage forKey:kCIInputImageKey];
        //设置模糊程度
        [filter setValue:@(radius) forKey: @"inputRadius"];
        CIImage *result = [filter valueForKey:kCIOutputImageKey];
        CGImageRef outImage = [context createCGImage: result fromRect:ciImage.extent];
        UIImage * blurImage = [UIImage imageWithCGImage:outImage];
        CGImageRelease(outImage);
        
        return blurImage;
    };
    
    [self setImageWithURL:[self handleImageURL:url]
              placeholder:nil
                  options:kNilOptions
                  manager:manager
                 progress:nil
                transform:nil
               completion:nil];
}

- (NSURL *)handleImageURL:(NSString *)url {
    NSString *imageURL = @"";
    
    if ([url hasPrefix:@"http"]) {
        imageURL = url;
    }else {
        imageURL = [NSString stringWithFormat:@"%@%@", BASEURL, url];
    }
    
    return [NSURL URLWithString:imageURL];
}



static UIView *_backView;
static UIImageView *_imageView;
static UIScrollView *_scorllView;
//原始尺寸
//static CGRect oldframe;

/**
 *  浏览大图
 */
- (void)zoomImagesOfTouchImageView {
    
    if (!self.image) {
//        [MBProgressHUD showMessage:@"图片不存在~" onView:[UIApplication sharedApplication].keyWindow];
        [MBProgressHUD showMessage:@"图片不存在~" toView:[UIApplication sharedApplication].keyWindow];
        return;
    }
        
    [self configUI];
    [self showImageView];
}

- (void)configUI {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _backView.backgroundColor = kRGBA(0x000000, 0.5);
    _backView.alpha = 0;
    [window addSubview:_backView];
    
    
    
    
    CGFloat height = self.image.size.height * kScreenWidth / self.image.size.width;
    
    _scorllView = [[UIScrollView alloc] initWithFrame:_backView.bounds];
    _scorllView.backgroundColor = [UIColor clearColor];
    _scorllView.userInteractionEnabled = YES;
    _scorllView.contentSize = CGSizeMake(kScreenWidth, height);
    _scorllView.minimumZoomScale = 0.5;
    _scorllView.maximumZoomScale = 3.f;
    _scorllView.delegate = self;
    [_scorllView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)]];
    [window addSubview:_scorllView];
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    //设置长按手势最小按压时间
    longPress.minimumPressDuration = 0.5;
    [_scorllView addGestureRecognizer:longPress];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
    _imageView.image = self.image;
    _imageView.alpha = 0;
    if (height < kScreenHeight) {
        _imageView.center = _scorllView.center;
    }
    [_scorllView addSubview:_imageView];

    
}

- (void)showImageView {
    
    CGSize size = self.image.size;
    CGFloat height = size.height * kScreenWidth / size.width;
    CGFloat y = height < kScreenHeight ? (kScreenHeight - height) / 2 : 0;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [_imageView setFrame:CGRectMake(0, y, kScreenWidth, height)];
        _imageView.alpha = 1;
        _backView.alpha = 1;
    }];
}

- (void)longPress:(UIGestureRecognizer *)gesture{
// 在手势开始时进行相关操作
    if (gesture.state == UIGestureRecognizerStateBegan) {
//        NSLog(@"在这里进行长按手势响应");
        
    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        
    }
}

/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
- (void)hideImageView:(UITapGestureRecognizer *)tap{
    //恢复
    [UIView animateWithDuration:0.4 animations:^{
        
        _imageView.alpha = 0;
        [_backView setAlpha:0];
        
    } completion:^(BOOL finished) {
        
        [_scorllView removeFromSuperview];
        [_backView removeFromSuperview];
        
    }];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // 延中心点缩放
    CGRect frame = _imageView.frame;
    
    frame.origin.y = (_scorllView.frame.size.height - _imageView.frame.size.height) > 0 ? (_scorllView.frame.size.height - _imageView.frame.size.height) * 0.5 : 0;
    frame.origin.x = (_scorllView.frame.size.width - _imageView.frame.size.width) > 0 ? (_scorllView.frame.size.width - _imageView.frame.size.width) * 0.5 : 0;
    _imageView.frame = frame;
    
    _scorllView.contentSize = CGSizeMake(_imageView.frame.size.width + 30, _imageView.frame.size.height + 30);

}

@end
